package com.airS.task.fileMgr.service.mgr.impl;

import com.airS.task.fileMgr.common.page.Page;
import com.airS.task.fileMgr.example.mgr.YunfileExample;
import com.airS.task.fileMgr.mapper.mgr.YunfileMapper;
import com.airS.task.fileMgr.model.mgr.Yunfile;
import com.airS.task.fileMgr.query.mgr.YunfileQuery;
import com.airS.task.fileMgr.service.mgr.YunfileService;
import com.airS.task.fileMgr.service.mgr.YunfileService;
import com.airS.task.fileMgr.utils.collection.CollectionUtils;
import com.airS.task.fileMgr.utils.page.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by alan on 2017/3/14.
 */
@Service
public class YunfileServiceImpl implements YunfileService {

    @Autowired
    private YunfileMapper yunfileMapper;

    @Override
    public Yunfile queryYunfileById(Integer id) {
        return yunfileMapper.selectByPrimaryKey(id);
    }

    @Override
    public Page<Yunfile> queryYunfiles(YunfileQuery query) {
        PageUtil.startPage(query);
        List<Yunfile> yunfiles = yunfileMapper.selectByExample(assemblyYunfileExample(query, null));
        return new Page<Yunfile>(yunfiles);
    }

    @Override
    public List<Yunfile> queryAllYunfiles(YunfileQuery query) {
        List<Yunfile> yunfiles = yunfileMapper.selectByExample(assemblyYunfileExample(query, null));
        return yunfiles;
    }

    @Override
    public List<Yunfile> queryAllYunfilesIn(YunfileQuery query, List<Integer> fileIds) {
        List<Yunfile> yunfiles = yunfileMapper.selectByExample(assemblyYunfileExample(query, fileIds));
        return yunfiles;
    }

    @Override
    public Integer addYunfile(Yunfile yunfile) {
        yunfileMapper.insert(yunfile);
        return yunfile.getFileId();
    }

    @Override
    public Integer updateYunfileById(Yunfile yunfile) {
        return yunfileMapper.updateByPrimaryKeySelective(yunfile);
    }

    @Override
    public Integer updateYunfileByExampleSelective(Yunfile yunfile, YunfileExample example) {
        return yunfileMapper.updateByExampleSelective(yunfile, example);
    }

    @Override
    public Integer delYunfileById(Integer id) {
        return yunfileMapper.deleteByPrimaryKey(id);
    }

    private YunfileExample assemblyYunfileExample(YunfileQuery query, List<Integer> fileIds){
        YunfileExample example = new YunfileExample();
        YunfileExample.Criteria criteria = example.createCriteria();
        if(StringUtils.hasText(query.getFileName())){
            criteria.andFileNameLike("%" + query.getFileName() + "%");
        }
        if(StringUtils.hasText(query.getFileType())){
            criteria.andFileTypeEqualTo(query.getFileType());
        }
        if(StringUtils.hasText(query.getGroupName())){
            criteria.andGroupNameEqualTo(query.getGroupName());
        }
        if(CollectionUtils.isNotEmpty(fileIds)){
            criteria.andFileIdIn(fileIds);
        }
        if(query.getParentId() != null){
            criteria.andParentIdEqualTo(query.getParentId());
        }
        if(query.getFileId() != null){
            criteria.andFileIdEqualTo(query.getFileId());
        }
        // 默认排序，按时间倒序
        example.setOrderByClause("create_time desc");
        return example;
    }
}
